"""
Case Type   : 事务-ustore
Case Name   : 事务->Create Table->回滚->kill数据库
Create At   : 2022/1/14
Owner       : @kyleze
Description :
    1.创建表并回滚
    # analyze {self.schema}.{self.table};
    2.kill数据库
    3.查询数据
Expect      :
    1.成功
    2.成功
    3.无法查询
History     :
"""
import os
import sys
import unittest

from yat.test import macro
from yat.test import Node

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class USTORE(unittest.TestCase):
    def setUp(self):
        self.pri_user = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.schema = 'schema_ustore_trans'
        self.table = 't_ustore_trans_0002'
        text = f'---{os.path.basename(__file__)} start---'
        self.log.info(text)
    
    def testunit_1(self):
        text = '----step1:创建表并回滚;expect:成功----'
        self.log.info(text)
        q_str = f'''drop table if exists {self.schema}.{self.table} cascade;
            start transaction;
            create table {self.schema}.{self.table}(c_id int,c_d_id int 
            not null,c_w_id int not null,c_first varchar(16) not null,
            c_middle char(22),c_last varchar(16) not null,c_street_1 
            varchar(20) not null,c_street_2 varchar(20),c_city varchar(20) 
            not null,c_state char(22) not null,c_zip char(9) not null,
            c_phone char(16) not null,c_since timestamp,c_credit char(22) 
            not null,c_credit_lim numeric(22,12),c_discount numeric(4,4),
            c_balance numeric(22,12),c_ytd_payment real not null,
            c_payment_cnt number not null,c_delivery_cnt bool not null,
            c_end date not null,c_data clob,c_clob clob) 
            with (storage_type=ustore);
            insert into {self.schema}.{self.table} 
            select * from {self.schema}.t_ustore_trans_0001;
            rollback;
            '''
        self.log.info(q_str)
        q_res = self.pri_sh.execut_db_sql(q_str)
        self.log.info(q_res)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, q_res,
              '执行失败: ' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, q_res,
                      '执行失败: ' + text)

        text = '----查询数据;expect:does not exist on datanode----'
        self.log.info(text)
        q_str = f'select count(*) from {self.schema}.{self.table};'
        self.log.info(q_str)
        q_res = self.pri_sh.execut_db_sql(q_str)
        self.log.info(q_res)
        self.assertIn("does not exist", q_res, '执行失败: ' + text)

        text = '----step2:kill-重启数据库;expect:成功----'
        self.log.info(text)
        result = self.pri_sh.restart_db_cluster()
        self.log.info(result)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue('Degraded' in status or 'Normal' in status)

        text = '----step3:查询数据;expect:does not exist on datanode----'
        self.log.info(text)
        q_str = f'select count(*) from {self.schema}.{self.table};'
        self.log.info(q_str)
        q_res = self.pri_sh.execut_db_sql(q_str)
        self.log.info(q_res)
        self.assertIn("does not exist", q_res, '执行失败: ' + text)

    def tearDown(self):
        text = '----step4:清理环境;expect:成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.schema}.{self.table} cascade;'
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result,
                      '执行失败: ' + text)
        text = f'---{os.path.basename(__file__)} end---'
        self.log.info(text)
